<template>
  <div class="page-wrapper">
    <header class="enhanced-header">
      <div class="header-left" @click="back">
        <i class="fas fa-arrow-left"></i>
        <span class="back-text">返回</span>
      </div>
      <h1 class="header-title">商家评价</h1>
    </header>

    <main class="page-main">
      <!-- 加载状态 -->
      <div v-if="loading" class="loading-indicator">
        <i class="fas fa-spinner fa-spin"></i> 正在加载评价...
      </div>

      <!-- 错误提示 -->
      <div v-else-if="error" class="error-alert">
        {{ error }}
      </div>

      <!-- 评价列表 -->
      <div v-else-if="comments.length" class="comments-container">
        <div 
          v-for="(comment, index) in comments"
          :key="index"
          class="comment-card animate__animated animate__fadeInUp"
        >
          <div class="user-info">
            <div class="avatar">
              <i class="fas fa-user-circle"></i>
            </div>
            <div class="meta">
              <span class="username">用户{{ index + 1 }}</span>
              <span class="timestamp">{{ getCurrentTime() }}</span>
            </div>
          </div>
          <p class="content">{{ comment }}</p>
        </div>
      </div>

      <!-- 空状态 -->
      <div v-else class="empty-state">
        <i class="fas fa-comments"></i>
        <p>暂时还没有用户评价</p>
      </div>
    </main>

    <Footer />
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'
import Footer from '../components/Footer.vue'

export default {
  name: 'BusinessReviews',
  components: { Footer },
  setup() {
    const router = useRouter()
    const comments = ref([])
    const loading = ref(false)
    const error = ref(null)
    const businessId = ref(null)

    // 获取当前时间（示例数据）
    const getCurrentTime = () => {
      return new Date().toISOString().split('T')[0]
    }

    // 获取商家信息
    const getBusinessInfo = () => {
      const business = JSON.parse(sessionStorage.getItem('business'))
      return business?.businessId || null
    }

    // 获取评价数据
    const fetchComments = async () => {
      try {
        loading.value = true
        error.value = null
        const response = await axios.get(
          `/OrdersController/getcomment/${businessId.value}`
        )
        comments.value = response.data.result || []
      } catch (err) {
        error.value = '获取评价失败，请稍后重试'
        comments.value = []
      } finally {
        loading.value = false
      }
    }

    // 初始化
    onMounted(() => {
      const id = getBusinessInfo()
      if (id) {
        businessId.value = id
        fetchComments()
      } else {
        error.value = '商家信息获取失败'
      }
    })

    return {
      back: () => router.go(-1),
      comments,
      loading,
      error,
      getCurrentTime
    }
  }
}
</script>


<style scoped>
/* 新增评价列表样式 */
.comments-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 1rem;
}

.comment-card {
  background: #fff;
  border-radius: 12px;
  padding: 1.5rem;
  margin-bottom: 1.5rem;
  box-shadow: 0 3px 10px rgba(0,0,0,0.1);
  transition: transform 0.2s;
}

.comment-card:hover {
  transform: translateY(-3px);
}

.user-info {
  display: flex;
  align-items: center;
  margin-bottom: 1rem;
}

.avatar {
  width: 40px;
  height: 40px;
  margin-right: 1rem;
}

.avatar i {
  font-size: 2.2rem;
  color: #007bff;
}

.meta {
  display: flex;
  flex-direction: column;
}

.username {
  font-weight: 500;
  color: #333;
}

.timestamp {
  font-size: 0.85rem;
  color: #999;
}

.content {
  font-size: 1.05rem;
  line-height: 1.6;
  color: #444;
  margin: 0;
  padding-left: 10px;
  border-left: 3px solid #eee;
}

/* 加载状态 */
.loading-indicator {
  text-align: center;
  padding: 3rem;
  color: #007bff;
  font-size: 1.1rem;
}

.fa-spinner {
  margin-right: 0.8rem;
}

/* 空状态 */
.empty-state {
  text-align: center;
  padding: 4rem 1rem;
  color: #888;
}

.empty-state i {
  font-size: 3.5rem;
  margin-bottom: 1.5rem;
  color: #e0e0e0;
}

.empty-state p {
  font-size: 1.2rem;
  margin: 0;
}

/* 错误提示 */
.error-alert {
  background: #ffe3e3;
  color: #dc3545;
  padding: 1.2rem;
  border-radius: 8px;
  text-align: center;
  margin: 2rem;
  border: 1px solid #ffc9c9;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .comments-container {
    padding: 0.5rem;
  }
  
  .comment-card {
    padding: 1rem;
    margin-bottom: 1rem;
  }
  
  .content {
    font-size: 1rem;
  }
}
</style>